package chapter08;

import lombok.extern.slf4j.Slf4j;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

@Slf4j
public class TimerTest {
    public static void main(String[] args) {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
        pool.schedule(()->{
            log.debug("task3");
        },5, TimeUnit.SECONDS);
        pool.schedule(()->{
            try {
                sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("task1");
        },5, TimeUnit.SECONDS);
        pool.schedule(()->{
            try {
                sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("task2");
        },5, TimeUnit.SECONDS);

        pool.schedule(()->{
            log.debug("task4");
        },5, TimeUnit.SECONDS);

    }

    private static void method1() {
        Timer timer = new Timer();
        TimerTask timerTask1 = new TimerTask() {
            @Override
            public void run() {
//                try {
//                    Thread.sleep(2000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                log.debug("task1");
            }
        };
        TimerTask timerTask2 = new TimerTask() {
            @Override
            public void run() {
                log.debug("task2");
            }
        };
        timer.schedule(timerTask1,1000);
        timer.schedule(timerTask2,1000);
    }
}
